package setting.huaqin.com.systemuiitemselect;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.PixelFormat;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by lubo on 17-12-8.
 */
public class DragView extends LinearLayout implements View.OnTouchListener{

    private final static String TAG="DragView";

    private int mHeight,mWidth;
    private ImageView mIV;
    private Context mContext;
    private HashMap<String,SystemUIItemBean> mMap;
    private List<TileView> mShowList,mHideList;
    private TileView mTileShow0,mTileShow1,mTileShow2,mTileShow3,mTileShow4,mTileShow5,mTileShow6,mTileShow7,mTileShow8
            ,mTileShow9,mTileShow10,mTileShow11;
    private TileView mTileHide0,mTileHide1,mTileHide2,mTileHide3;
    private FrameLayout mTouchPanel;
    private LinearLayout mIconLayout;
    private TileView selectedTile=null;
    private WindowManager mWindowManager;
    private WindowManager.LayoutParams mParams;
    private int statusBarHeight,actionBarHeight;
    private int selectPosition,targetPosition;

    public DragView(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.drag_view, this);
    }

    public DragView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.e(TAG, "init ");
        init(context);
    }

    public DragView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.drag_view, this);
    }

    public DragView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private void init(Context context){
        mContext=context;
        LayoutInflater.from(mContext).inflate(R.layout.drag_view, this);
        mIconLayout=(LinearLayout)findViewById(R.id.icon_layout);
        mIV=new ImageView(mContext);
        mIV.setBackgroundResource(R.drawable.ic_sequence_airplane);
        statusBarHeight=72;//need update
        actionBarHeight=168;//need update
        mMap=new HashMap<>();
        mShowList=new ArrayList<>();
        mHideList=new ArrayList<>();
        mTileShow0=(TileView)findViewById(R.id.line0_0);
        mTileShow1=(TileView)findViewById(R.id.line0_1);
        mTileShow2=(TileView)findViewById(R.id.line0_2);
        mTileShow3=(TileView)findViewById(R.id.line0_3);
        mTileShow4=(TileView)findViewById(R.id.line1_0);
        mTileShow5=(TileView)findViewById(R.id.line1_1);
        mTileShow6=(TileView)findViewById(R.id.line1_2);
        mTileShow7=(TileView)findViewById(R.id.line1_3);
        mTileShow8=(TileView)findViewById(R.id.line2_0);
        mTileShow9=(TileView)findViewById(R.id.line2_1);
        mTileShow10=(TileView)findViewById(R.id.line2_2);
        mTileShow11=(TileView)findViewById(R.id.line2_3);
        mTileHide0=(TileView)findViewById(R.id.line3_0);
        mTileHide1=(TileView)findViewById(R.id.line3_1);
        mTileHide2=(TileView)findViewById(R.id.line3_2);
        mTileHide3=(TileView)findViewById(R.id.line3_3);
        mShowList.add(mTileShow0);
        mShowList.add(mTileShow1);
        mShowList.add(mTileShow2);
        mShowList.add(mTileShow3);
        mShowList.add(mTileShow4);
        mShowList.add(mTileShow5);
        mShowList.add(mTileShow6);
        mShowList.add(mTileShow7);
        mShowList.add(mTileShow8);
        mShowList.add(mTileShow9);
        mShowList.add(mTileShow10);
        mShowList.add(mTileShow11);
        mHideList.add(mTileHide0);
        mHideList.add(mTileHide1);
        mHideList.add(mTileHide2);
        mHideList.add(mTileHide3);
        initTiles();
        initFloat();
    }

    private void initFloat(){
        mWindowManager=(WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
        mParams=new WindowManager.LayoutParams();
        mParams.gravity=Gravity.TOP | Gravity.LEFT;
        mParams.type=WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        mParams.format = PixelFormat.TRANSPARENT;
        mParams.width = 100;
        mParams.height = 100;
    }

    private void initTiles(){
        String show = mContext.getResources().getString(R.string.quick_settings_tiles_default_show);
        String[] showTiles=show.split(",");
        String hide = mContext.getResources().getString(R.string.quick_settings_tiles_default_hide);
        String[] hideTiles=hide.split(",");
        Resources res=mContext.getResources();

        SystemUIItemBean nfcBean=new SystemUIItemBean();
        nfcBean.setmName(res.getString(R.string.sequence_nfc));
        nfcBean.setmIcon(R.drawable.ic_sequence_nfc);
        mMap.put(TileContanst.TILE_NFC, nfcBean);

        SystemUIItemBean gpsBean=new SystemUIItemBean();
        gpsBean.setmName(res.getString(R.string.sequence_gps));
        gpsBean.setmIcon(R.drawable.ic_sequence_gps);
        mMap.put(TileContanst.TILE_LOCATION, gpsBean);

        SystemUIItemBean dataBean=new SystemUIItemBean();
        dataBean.setmName(res.getString(R.string.sequence_data));
        dataBean.setmIcon(R.drawable.ic_sequence_data);
        mMap.put(TileContanst.TILE_DATACONNECTION, dataBean);

        SystemUIItemBean hotSpotBean=new SystemUIItemBean();
        hotSpotBean.setmName(res.getString(R.string.sequence_wifi_hotspot));
        hotSpotBean.setmIcon(R.drawable.ic_sequence_hotspot);
        mMap.put(TileContanst.TILE_HOTSPOT, hotSpotBean);

        SystemUIItemBean batteryBean=new SystemUIItemBean();
        batteryBean.setmName(res.getString(R.string.sequence_power_saving_mode));
        batteryBean.setmIcon(R.drawable.ic_sequence_power_save);
        mMap.put(TileContanst.TILE_NORMALMODE, batteryBean);

        SystemUIItemBean scanBean=new SystemUIItemBean();
        scanBean.setmName(res.getString(R.string.sequence_scan));
        scanBean.setmIcon(R.drawable.ic_sequence_scan);
        mMap.put(TileContanst.TILE_SCAN, scanBean);

        SystemUIItemBean btBean=new SystemUIItemBean();
        btBean.setmName(res.getString(R.string.sequence_bluetooth));
        btBean.setmIcon(R.drawable.ic_sequence_bluetooth);
        mMap.put(TileContanst.TILE_BT, btBean);

        SystemUIItemBean screenShotBean=new SystemUIItemBean();
        screenShotBean.setmName(res.getString(R.string.sequence_screenshot));
        screenShotBean.setmIcon(R.drawable.ic_sequence_screenshot);
        mMap.put(TileContanst.TILE_SCREENSHOT, screenShotBean);

        SystemUIItemBean audioBean=new SystemUIItemBean();
        audioBean.setmName(res.getString(R.string.sequence_ringtone));
        audioBean.setmIcon(R.drawable.ic_sequence_ringtone);
        mMap.put(TileContanst.TILE_AUDIOPROFILE, audioBean);

        SystemUIItemBean flashlightBean=new SystemUIItemBean();
        flashlightBean.setmName(res.getString(R.string.sequence_flashlight));
        flashlightBean.setmIcon(R.drawable.ic_sequence_flashlight);
        mMap.put(TileContanst.TILE_FLASHLIGHT, flashlightBean);

        SystemUIItemBean rotateBean=new SystemUIItemBean();
        rotateBean.setmName(res.getString(R.string.sequence_rotate));
        rotateBean.setmIcon(R.drawable.ic_sequence_rotate);
        mMap.put(TileContanst.TILE_ROTATION, rotateBean);

        SystemUIItemBean moreBean=new SystemUIItemBean();
        moreBean.setmName(res.getString(R.string.sequence_more));
        moreBean.setmIcon(R.drawable.ic_sequence_more);
        mMap.put(TileContanst.TILE_TILESORT, moreBean);

        SystemUIItemBean ndbBean=new SystemUIItemBean();
        ndbBean.setmName(res.getString(R.string.sequence_not_disturb));
        ndbBean.setmIcon(R.drawable.ic_sequence_not_disturb);
        mMap.put(TileContanst.TILE_NOTDISTURB, ndbBean);

        SystemUIItemBean airBean=new SystemUIItemBean();
        airBean.setmName(res.getString(R.string.sequence_airplane));
        airBean.setmIcon(R.drawable.ic_sequence_airplane);
        mMap.put(TileContanst.TILE_AIRPLANE, airBean);

        SystemUIItemBean eyeBean=new SystemUIItemBean();
        eyeBean.setmName(res.getString(R.string.sequence_protect_eyes));
        eyeBean.setmIcon(R.drawable.ic_sequence_protect_eyes);
        mMap.put(TileContanst.TILE_EYEMODE, airBean);

        SystemUIItemBean WIFIBean=new SystemUIItemBean();
        WIFIBean.setmName(res.getString(R.string.sequence_wlan));
        WIFIBean.setmIcon(R.drawable.ic_sequence_wlan);
        mMap.put(TileContanst.TILE_WIFI,WIFIBean);
        int i=0;
        for(String tile :showTiles){
                SystemUIItemBean bean=mMap.get(tile);
                TileView tileView=mShowList.get(i++);
                tileView.setTileIcon(bean.getmIcon());
                tileView.setTileTitle(bean.getmName());
                tileView.setOnTouchListener(this);
        }
        i=0;
        for(String tile :hideTiles){
            SystemUIItemBean bean=mMap.get(tile);
            TileView tileView=mHideList.get(i++);
            tileView.setTileIcon(bean.getmIcon());
            tileView.setTileTitle(bean.getmName());
            tileView.setOnTouchListener(this);
        }
    }
/*
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        Log.e(TAG, "left:" + i + " top:" + i1+" right:"+i2+" bottom:"+i3);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
*/


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                mParams.x=(int)event.getRawX()-(270/2);
                mParams.y=(int)event.getRawY()-(60/2);
                mWindowManager.addView(mIV,mParams);
                break;
            case MotionEvent.ACTION_MOVE:
                mParams.x=(int)event.getRawX()-(270/2);
                mParams.y=(int)event.getRawY()-(60/2);
                mWindowManager.updateViewLayout(mIV,mParams);
                break;
            case MotionEvent.ACTION_UP:
                mWindowManager.removeView(mIV);
                Log.e(TAG, "position=========>>>>>" + getPosition(event));
                if(selectedTile==null){
                    return true;
                }
                selectedTile.setVisibility(VISIBLE);
                int position=getPosition(event);
                targetPosition=position;
                if(selectPosition!=-1 && targetPosition!=-1) {
                    changePosition(selectPosition, targetPosition);
                }
                break;
        }
        return true;
    }

    public void loadView(){
        int height=this.getMeasuredHeight();
        int width=this.getMeasuredWidth();
        if(height!=0 && width!=0){
            mHeight=height;
            mWidth=width;
            Log.e(TAG,"width:"+mWidth+" height:"+mHeight);
        }
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if(view instanceof TileView){
            Log.e(TAG, "touch=======>>>" + ((TileView) view).getTileTitle()+" position:"+getPosition(motionEvent));
            selectedTile=(TileView)view;
            selectedTile.setVisibility(INVISIBLE);
            selectPosition=getPosition(motionEvent);
        }
        return false;
    }

    private int getPosition(MotionEvent event){
        int position=-1;
        int y=(int)event.getRawY();
        int x=(int)event.getRawX();
        position=(x/270)+(((y-statusBarHeight-actionBarHeight)/240)*4);
        if(position>15)position=-1;
        return position;
    }

    private void changePosition(int selectPosition,int targetPosition){
        TileView selectView = null,targetView = null;
        int exchangeIconId;
        String exchangTitle;
        if(selectPosition<12){
            selectView=mShowList.get(targetPosition);
        }else if(selectPosition>11 && selectPosition<16){
            selectView=mHideList.get(selectPosition-12);
        }

        exchangeIconId=selectView.getIconId();
        exchangTitle=selectView.getTileTitle();

        if(targetPosition<12){
            targetView=mShowList.get(targetPosition);
        }else if(targetPosition>11 && targetPosition<16){
            targetView=mHideList.get(targetPosition-12);
        }

        selectView.setTileIcon(targetView.getIconId());
        selectView.setTileTitle(targetView.getTileTitle());
        targetView.setTileIcon(exchangeIconId);
        targetView.setTileTitle(exchangTitle);
        selectView.invalidate();
        targetView.invalidate();
    }

}
